package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.ParkPointBound;
import com.tbit.uqbike.service.business.ParkFunctionService;
import com.tbit.uqbike.webmanager.dao.core.ParkPointBoundDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ParkFunctionServiceImpl implements ParkFunctionService {
    @Autowired
    private ParkPointBoundDao parkPointBoundDao;

    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(new Date()));
    }

    @Override
    public List<ParkPointBound> queryPage(Integer state, Integer parkPointId, String machineNO, Integer type, Integer pageNO, Integer rowCount) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        params.put("parkPointId", parkPointId);
        params.put("state", state);
        params.put("machineNO", machineNO);
        params.put("pageNO", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);
        return parkPointBoundDao.queryPage(params);
    }

    @Override
    public Integer queryCount(Integer state, Integer parkPointId, String machineNO, Integer type) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        params.put("parkPointId", parkPointId);
        params.put("state", state);
        params.put("machineNO", machineNO);
        return parkPointBoundDao.queryCount(params);
    }

    @Override
    public void insert(ParkPointBound parkPointBound) {
        parkPointBound.setMachineNo(parkPointBound.getFunctionNo());
        ParkPointBound parkPointBoundDB = parkPointBoundDao.getByNO(parkPointBound.getMachineNo());
        if (null != parkPointBoundDB) {
            throw new BaseException("ver.parkFunection.exist");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        parkPointBound.setAddTime(simpleDateFormat.format(new Date()));
        parkPointBound.setState(0);
        parkPointBound.setType(1);
        parkPointBoundDao.insert(parkPointBound);
    }

    @Override
    public void addBatch(Integer parkPointId, List<String> functionNos) {
        /**检查是否存在*/
        List<String> exitfunctionNos = parkPointBoundDao.getByMachineNos(functionNos);
        if (exitfunctionNos.size() > 0) {
            throw new BaseException("这批设备编号中已经存在设备，请检查");
        }
        List<ParkPointBound> list = new LinkedList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (String functionNo : functionNos) {
            ParkPointBound parkPointBound = new ParkPointBound();
            parkPointBound.setType(1);
            parkPointBound.setState(0);
            parkPointBound.setAddTime(simpleDateFormat.format(new Date()));
            parkPointBound.setMachineNo(functionNo);
            parkPointBound.setParkPointId(parkPointId);
            list.add(parkPointBound);
        }
        parkPointBoundDao.addBatch(list);
    }

    @Override
    public void del(String functionNo) {
        parkPointBoundDao.del(functionNo);
    }
}
